package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.ReportOrder;
import com.sky.dto.statusOrder;
import com.sky.entity.Orders;
import com.sky.entity.WorkOrder;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService
{
    @Autowired
    private OrderMapper orderMapper;

//    @Autowired
//    private OrderDetailMapper orderDetailMapper;


    //用户创建订单
    public Orders submitOrder(OrdersDTO ordersDTO) {
        Orders orders=new Orders();
        BeanUtils.copyProperties(ordersDTO,orders);
        orders.setStatus(orders.ORDER_WITHOUT);
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        orders.setOrderTime(LocalDateTime.now());
        orderMapper.insert(orders);
        return orders;
    }

    //查询订单
    public ArrayList<WorkOrder> selectOrder(ReportOrder reportOrder) {
        ArrayList<WorkOrder> arrayList=new ArrayList<>();
        arrayList = orderMapper.selectOrder(reportOrder);
        return arrayList;
    }
    //工人接单
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = Orders.builder()
                .id(ordersConfirmDTO.getId())
                .status(Orders.CONFIRMED)
                .build();

        orderMapper.update(orders);
    }

    //用户催单
    public void remind(Long id) {
        orderMapper.remind(id);
    }

    //学生查询订单
    public PageResult selectUserOrder(statusOrder statusOrder) {
        ArrayList<Orders> arrayList=new ArrayList<>();
        arrayList= orderMapper.selectsUserOrder(statusOrder);
        return new PageResult(statusOrder.getStatus(),arrayList);
    }

    //工人完成订单
    public void finish(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = Orders.builder()
                .id(ordersConfirmDTO.getId())
                .status(Orders.ORDER_REPAIR_OVER)
                .build();
        orderMapper.updateFinish(orders);
    }

    //工人批量接单
    public void batchUpdateOrders(List<Long> orderIds) {
        orderIds.forEach(reportNumber -> {
            orderMapper.batchUpdateOrders(reportNumber);
        });

    }

    //工人批量完成订单
    public void batchUpdateOrdersfinish(List<Long> orderIds) {
        orderIds.forEach(reportNumber -> {
            orderMapper.batchUpdateOrdersfinish(reportNumber);
        });
    }

    //查询各状态订单
    public ArrayList<WorkOrder> selectStatusOrders(String status) {
        ArrayList<WorkOrder> arrayList=new ArrayList<>();
        arrayList = orderMapper.selectStatusOrders(status);
        return arrayList;
    }
}
